<?php
namespace Mouta\AmistateBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ImportImagesCommand extends Command
{
    protected function configure()
    {
        $this
            ->setName('amistate:importImages')
            ->setDescription('Importa las imagens del sitio de produccion')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
		$em = $this->getApplication()->getKernel()->getContainer()->get('doctrine')->getEntityManager();
		//$em = new \Doctrine\ORM\EntityManager();
		$images = $em->getRepository('AmistateBundle:Image')->findAll();
        foreach($images as $image)
		{
			$path = $image->getPath();
			$aux = explode('.', $path);
			
			$filename = sha1(uniqid(mt_rand(), true));
			$folder = ceil($image->getUsuario()->getId() / 1000);
            $newPath = $filename.'.'.$aux[1];
			$file = "http://www.amistate.com/upload/$folder/{$image->getUsuario()->getId()}/$path";
			$imgFile = file_get_contents($file);
			
			$image->setPath($newPath);
			file_put_contents($image->getAbsolutePath(), $imgFile);
			$em->persist($image);
			$em->flush();
			$output->write('Imagen Procesada: '.$image->getId(),true);
		}
    }
}